﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong_Project.Views
{
    class Menu : View
    {
        private string header, helpString, aboutString, startGameString, quitGameString, settingsString;
        private SpriteFont settings, help, about, startGame, ultimatePong, quitGame;
        private Vector2 settingsPos, helpPos, aboutPos, startGamePos, quitGamePos, headerPos;
        private Vector2 settingsOrigin, HelpOrigin, aboutPrigin, startGameorigin, quitGameOrigin, headerOrigin;
        private int selectedEntry = 1;
        private int minEntry = 1;
        private int maxEntry = 5;
        private Song bgm;

        public Menu()
            : base()
        {
            startGameString = "New Game";
            startGamePos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 150);
            settingsString = "Settings";
            settingsPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 125);
            aboutString = "About";
            aboutPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 100);
            helpString = "Help";
            helpPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 75);
            quitGameString = "Quit";
            quitGamePos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 50);
            header = "ULTIMATE PONG";
            headerPos = new Vector2(Game.WindowWidth / 2, 30);
        }

        public override void LoadContent(ContentManager content)
        {
            ultimatePong = content.Load<SpriteFont>("UltimatePong");
            headerOrigin = ultimatePong.MeasureString(header) / 2;

            //LoadContent(content);
            settings = content.Load<SpriteFont>("Settings");
            settingsOrigin = settings.MeasureString(settingsString) / 2;

            help = content.Load<SpriteFont>("Help");
            HelpOrigin = help.MeasureString(helpString) / 2;

            startGame = content.Load<SpriteFont>("NewGame");
            startGameorigin = help.MeasureString(startGameString) / 2;

            about = content.Load<SpriteFont>("About");
            aboutPrigin = about.MeasureString(aboutString) / 2;

            quitGame = content.Load<SpriteFont>("Quit");
            quitGameOrigin = quitGame.MeasureString(quitGameString) / 2;

            bgm = content.Load<Song>("02_Ridge_Racer");

            try
            {
                MediaPlayer.Play(bgm);
                MediaPlayer.IsRepeating = true;
                MediaPlayer.IsMuted = true;
            }
            catch (InvalidOperationException)
            {
                // Ignore background music failure
            }

        }

        public override void Update(GameTime gameTime)
        {
            if (Controls.MuteSound)
            {
                MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            }

            if (Controls.MenuDown)
            {
                if (selectedEntry + 1 > maxEntry)
                    selectedEntry = minEntry;
                else
                    selectedEntry++;
            }
            else if (Controls.MenuUp)
            {
                if (selectedEntry - 1 < minEntry)
                    selectedEntry = maxEntry;
                else
                    selectedEntry--;
            }

            if (Controls.Enter)
            {
                switch (selectedEntry)
                {
                    case 1:
                        ViewManager.State = GameState.InGame;
                        GameObjectManager.ResetObjects();
                        Score.GetInstance().Reset();
                        break;
                    case 2:
                        ViewManager.State = GameState.Settings;
                        break;
                    case 3:
                        ViewManager.State = GameState.About;
                        break;
                    case 4:
                        ViewManager.State = GameState.Help;
                        break;
                    case 5:
                        Game.GetInstance().Exit();
                        break;
                    default:
                        throw new ArgumentException("Invalid menu entry: " + selectedEntry);
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(ultimatePong, header, headerPos, Color.White, 0, headerOrigin, 2.0f, SpriteEffects.None, 0.5f);

            if (selectedEntry != 1)
                spriteBatch.DrawString(startGame, startGameString, startGamePos, Color.White, 0, startGameorigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 2)
                spriteBatch.DrawString(settings, settingsString, settingsPos, Color.White, 0, settingsOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 3)
                spriteBatch.DrawString(about, aboutString, aboutPos, Color.White, 0, aboutPrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 4)
                spriteBatch.DrawString(help, helpString, helpPos, Color.White, 0, HelpOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 5)
                spriteBatch.DrawString(quitGame, quitGameString, quitGamePos, Color.White, 0, quitGameOrigin, 1.0f, SpriteEffects.None, 0.5f);

            switch (selectedEntry)
            {
                case 1:
                    spriteBatch.DrawString(startGame, startGameString, startGamePos, Color.Red, 0, startGameorigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 2:
                    spriteBatch.DrawString(settings, settingsString, settingsPos, Color.Red, 0, settingsOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 3:
                    spriteBatch.DrawString(about, aboutString, aboutPos, Color.Red, 0, aboutPrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 4:
                    spriteBatch.DrawString(help, helpString, helpPos, Color.Red, 0, HelpOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 5:
                    spriteBatch.DrawString(quitGame, quitGameString, quitGamePos, Color.Red, 0, quitGameOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                default:
                    throw new ArgumentException("Invalid menu entry: " + selectedEntry);
            }
        }
    }
}
